import { getAllPlatform } from '@hs-server/api'
import { Flex, Tree } from 'antd'
import { useEffect, useState } from 'react'

interface Props {
  record: any
  updateChecked: (checkedValue: string[]) => void
  type: string
}
const PublishPlatform = (props: Props) => {
  //type: Database:数据库查询配置 IndustryChain:产业链查询配置
  const { record, updateChecked, type } = props
  const [allPlatform, setallPlatform] = useState<any[]>([])
  const [checkedKeys, setcheckedKeys] = useState<string[]>([])

  const onCheck: any = (latestCheckedKeys: string[]) => {
    setcheckedKeys(latestCheckedKeys)
    updateChecked(latestCheckedKeys)
  }

  useEffect(() => {
    getAllPlatform({ use_for: type === 'Database' ? 'data_item' : 'industry' }).then((res) => {
      setallPlatform(res)
      const {
        release_info: { platforms = [] }
      } = record
      if (platforms.length) {
        setcheckedKeys(platforms.map((item: { id: any }) => item.id))
      }
    })
  }, [record, type])

  return (
    <Flex vertical>
      <Tree
        checkable
        onCheck={onCheck}
        treeData={allPlatform}
        checkedKeys={checkedKeys}
        defaultExpandAll
        fieldNames={{ title: 'label', key: 'value' }}
      />
    </Flex>
  )
}

export default PublishPlatform
